package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.domain.Profit;
import com.tjetc.service.ProfitService;
import com.tjetc.service.impl.ProfitServiceImpl;
import lombok.Data;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;


@WebServlet("/profit/*")
@Data
public class ProfitServlet extends HttpServlet {
    private ProfitService profitService=new ProfitServiceImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Profit> list=profitService.list();
        new ObjectMapper().writeValue(response.getOutputStream(),list);
    }

    protected void ceshi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String aa=request.getParameter("value");
        String[] dates = aa.split(",");
        String startDate= dates[0];
        System.out.println("startDate = " + startDate);
        String endDate = dates[1];
        System.out.println("endDate = " + endDate);
        List<Profit> list=profitService.listbydate(startDate,endDate);
        new ObjectMapper().writeValue(response.getOutputStream(),list);
    }
}
